﻿using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using SlXnaApp1.GameObjects;
using SlXnaApp1.ViewModel;

namespace SlXnaApp1
{
    public partial class GamePage
    {
        readonly ContentManager _contentManager;
        readonly GameTimer _timer;
        SpriteBatch _spriteBatch;

        // For rendering the XAML onto a texture
        UIElementRenderer _elementRenderer;

        public GamePage()
        {
            DataContext = new GamePageViewModel();
            InitializeComponent();
            // Get the content manager from the application
            _contentManager = ((App) Application.Current).Content;
            InitLoyoutRoot();
            // Create a timer for this page
            _timer = new GameTimer {UpdateInterval = TimeSpan.FromTicks(333333)};
            _timer.Update += OnUpdate;
            _timer.Draw += OnDraw;
        }

        private void InitLoyoutRoot()
        {
            LayoutRoot.Visibility = Visibility.Collapsed;
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // TODO: use this.content to load your game content here
            InitGameObject();
            // Start the timer
            _timer.Start();

            base.OnNavigatedTo(e);
        }

        private void InitGameObject()
        {
            var texture = _contentManager.Load<Texture2D>("clickobj");
            var tempBtn = new TargetBtn(_spriteBatch, texture);
            tempBtn.FinishEvent += TempBtnFinishEvent;
            SilverLaoyuot.Tap += tempBtn.OnTap;
            _timer.Draw += tempBtn.OnDraw;
            _timer.Update += tempBtn.OnUpdate;
        }

        

        void TempBtnFinishEvent(object sender, TargetBtn.FinishEventArgs e)
        {
            scoreTxt.Text = Convert.ToString(e.Score);
            _timer.Stop();
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            LayoutRoot.Visibility = Visibility.Visible; 
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            _timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here

        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {

            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Transparent);
           if (null == _elementRenderer)
           {
               _elementRenderer = new UIElementRenderer(this, (int)ActualWidth, (int)ActualHeight);

           }

           _elementRenderer.Render();
           // Render the Silverlight controls using the UIElementRenderer.
           
            // TODO: Add your drawing code here
           _spriteBatch.Begin();
           _spriteBatch.Draw(_elementRenderer.Texture, new Vector2(0, 0), Color.Transparent);
           _spriteBatch.End();

        }

        private void ButtonClickGoBack(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }


    }
}